﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;

namespace RefLib.Branding
{
    internal class BrandingContext : ObjectContext
    {
        public static readonly string BrandingConnection = "BrandingConnection";

        public BrandingContext(EntityConnection connection)
        : base(connection)
        {
        }

        public List<BrandedObjectDTO> GetAllObjects(string tag, string subTag, int deferredLoadingLength)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.GetAllObjects @tag, @subTag, @deferredLoadingLength", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "tag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(tag) ? DBNull.Value : (object) tag },
                    new SqlParameter() { ParameterName = "subTag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(subTag) ? DBNull.Value : (object) subTag },
                    new SqlParameter() { ParameterName = "deferredLoadingLength", SqlDbType = SqlDbType.Int, Value = deferredLoadingLength }
                }).ToList();
        }

        public BrandedObjectDTO GetBlobByGuid(Guid blobGuid)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.GetBlobByGuid @blobGuid", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "blobGuid", SqlDbType = SqlDbType.UniqueIdentifier, Value = blobGuid }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO GetBlobById(int blobId)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.GetBlobById @blobId", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "blobId", SqlDbType = SqlDbType.Int, Value = blobId }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO GetTextById(int textId)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.GetTextById @textId", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "textId", SqlDbType = SqlDbType.Int, Value = textId }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO GetValueById(int valueId)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.GetValueById @valueId", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "valueId", SqlDbType = SqlDbType.Int, Value = valueId }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO ResolveBlob(string resourceId, string tag, string subTag, string targetBrand)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.ResolveBlob @resourceId, @tag, @subTag, @brand", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "resourceId", SqlDbType = SqlDbType.VarChar, Size = 200, Value = resourceId },
                    new SqlParameter() { ParameterName = "tag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(tag) ? DBNull.Value : (object) tag },
                    new SqlParameter() { ParameterName = "subTag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(subTag) ? DBNull.Value : (object) subTag },
                    new SqlParameter() { ParameterName = "brand", SqlDbType = SqlDbType.VarChar, Size = 200, Value = targetBrand }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO ResolveText(string resourceId, string tag, string subTag, string targetBrand)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.ResolveText @resourceId, @tag, @subTag, @brand", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "resourceId", SqlDbType = SqlDbType.VarChar, Size = 200, Value = resourceId },
                    new SqlParameter() { ParameterName = "tag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(tag) ? DBNull.Value : (object) tag },
                    new SqlParameter() { ParameterName = "subTag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(subTag) ? DBNull.Value : (object) subTag },
                    new SqlParameter() { ParameterName = "brand", SqlDbType = SqlDbType.VarChar, Size = 200, Value = targetBrand }
                }).SingleOrDefault();
        }

        public BrandedObjectDTO ResolveValue(string resourceId, string tag, string subTag, string targetBrand)
        {
            return ExecuteStoreQuery<BrandedObjectDTO>("branding.ResolveValue @resourceId, @tag, @subTag, @brand", new SqlParameter[]
                {
                    new SqlParameter() { ParameterName = "resourceId", SqlDbType = SqlDbType.VarChar, Size = 200, Value = resourceId },
                    new SqlParameter() { ParameterName = "tag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(tag) ? DBNull.Value : (object) tag },
                    new SqlParameter() { ParameterName = "subTag", SqlDbType = SqlDbType.VarChar, Size = 50, Value = String.IsNullOrEmpty(subTag) ? DBNull.Value : (object) subTag },
                    new SqlParameter() { ParameterName = "brand", SqlDbType = SqlDbType.VarChar, Size = 200, Value = targetBrand }
                }).SingleOrDefault();
        }
    }
}
